package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pub.MessageConstant;
import com.itheima.service.CheckitemService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkitem")
public class CheckitemController {

    @Reference
    CheckitemService checkitemService;

    // 分页查询检查项
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = checkitemService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    // 添加或者更新检查项
    @RequestMapping("addCheckItem")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            checkitemService.saveOrUpdate(checkItem);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKITEM_FAIL);
        }
    }

    // 根据id值删除检查项
    @RequestMapping("deleteCheckitemById")
    public Result deleteCheckitemById(@RequestParam("id") Integer id){
        try {
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1);
            // 目前来看此种方法最适合进行逻辑删除  值为null的不更新,  那为0或者0.0呢??(price变量 属性为float 为什么不更新)
            checkitemService.updateById(checkItem);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    // 这是用在检查组页面新建及编辑检查组用来显示所有检查项的方法
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<CheckItem> list = checkitemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
